package com.dbuil.bleshoes.utils.whiteutil;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;

import androidx.annotation.NonNull;

public class WhiteUtils {

   private Context context;

    public WhiteUtils(Context context) {
        this.context = context;
    }

    /**

     * 跳转到指定应用的首页

     */

    private void showActivity(@NonNull String packageName) {

        Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);

        context.startActivity(intent);

    }



    /**

     * 跳转到指定应用的指定页面

     */

    private void showActivity(@NonNull String packageName, @NonNull String activityDir) {

        Intent intent = new Intent();

        intent.setComponent(new ComponentName(packageName, activityDir));

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        context.startActivity(intent);

    }

    public void select(){

        if (isHuawei()) {
            goHuaweiSetting(context);
        }else
        if (isXiaomi()){
            goXiaomiSetting();
        }else
        if (isMeizu()) {
            goMeizuSetting();
        }else
        if (isOPPO()) {
            goOPPOSetting();
        }else
        if (isSamsung()) {
            goSamsungSetting();
        }else
        if (isVIVO()) {
            goVIVOSetting();
        }else{
            //默认跳转到应用设置页面
            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            intent.setData(Uri.fromParts("package", context.getPackageName(), null));
            context.startActivity(intent);
        }


    }


    public boolean isHuawei() {

        if(Build.BRAND == null) {

            return false;

        } else{

            return Build.BRAND.toLowerCase().equals("huawei") || Build.BRAND.toLowerCase().equals("honor");

        }

    }

    private void goHuaweiSetting(Context context) {

        try{

            showActivity("com.huawei.systemmanager",

                    "com.huawei.systemmanager.startupmgr.ui.StartupNormalAppListActivity");

        } catch(Exception e) {

            showActivity("com.huawei.systemmanager",

                    "com.huawei.systemmanager.optimize.bootstart.BootStartActivity");

        }

    }


    public static boolean isXiaomi() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("xiaomi");

    }

    private void goXiaomiSetting() {

        showActivity("com.miui.securitycenter",

                "com.miui.permcenter.autostart.AutoStartManagementActivity");

    }


    public static boolean isOPPO() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("oppo");

    }


    private void goOPPOSetting() {

        try{

            showActivity("com.coloros.phonemanager");

        } catch(Exception e1) {

            try{

                showActivity("com.oppo.safe");

            } catch(Exception e2) {

                try{

                    showActivity("com.coloros.oppoguardelf");

                } catch(Exception e3) {

                    showActivity("com.coloros.safecenter");

                }

            }

        }

    }


    public static boolean isVIVO() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("vivo");

    }


    private void goVIVOSetting() {

        showActivity("com.iqoo.secure");

    }


    public static boolean isMeizu() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("meizu");

    }


    private void goMeizuSetting() {

        showActivity("com.meizu.safe");

    }

    public static boolean isSamsung() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("samsung");

    }


    private void goSamsungSetting() {

        try{

            showActivity("com.samsung.android.sm_cn");

        } catch(Exception e) {

            showActivity("com.samsung.android.sm");

        }

    }

    public static boolean isLeTV() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("letv");

    }


    private void goLetvSetting() {

        showActivity("com.letv.android.letvsafe",

                "com.letv.android.letvsafe.AutobootManageActivity");

    }


    public static boolean isSmartisan() {

        return Build.BRAND != null&& Build.BRAND.toLowerCase().equals("smartisan");

    }


    private void goSmartisanSetting() {

        showActivity("com.smartisanos.security");

    }


}
